const Router=require('koa-router');
const game=new Router(); 
const sqldata=require('../sqldata')
//诗词接龙
game.get('/game1',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from game1,user where game1.uid=user.uid`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
game.post('/game1',async(ctx)=>{
    let val=ctx.request.body
    let sql=`insert into game1 (uid,gcontent,gtime) values ('${val.uid}','${val.gcontent}','${val.gtime}')`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//飞花令
game.get('/game2/title',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from title2`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
game.get('/game2/detail',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from game2,user where fid=${val.fid} and game2.uid=user.uid`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
game.post('/game2/detail',async(ctx)=>{
    let val=ctx.request.body
    let sql=`insert into game2 (fid,uid,fcontent,ftime) values (${val.fid},'${val.uid}','${val.fcontent}','${val.ftime}')`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
//点字成诗
game.get('/game3/detail',async(ctx)=>{
    let val=ctx.request.query
    let sql=`select * from game3`
    let mydata = await new Promise((resolve, reject)=>{
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body=mydata;
})
module.exports=game;